package com.example.demo.controller;

import com.example.demo.pojo.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
@Tag(name = "request",description = "request")

public class RequestController {
//    //原始方式
//    @RequestMapping("/simplParm")
//    public String simplParm(HttpServletRequest request){
//        String name = request.getParameter("name");
//        String ageStr = request.getParameter("age");
//        int age = Integer.parseInt(ageStr);
//        System.out.println(name + ":" + age);
//        return "success";
//    }

    //springboot方式
    @RequestMapping(value = "/simpleParm",method = RequestMethod.GET)
    @Operation(description = "springboot")
    public String simplParm(@RequestParam(name = "name",required = false) String name, Integer age){
        System.out.println(name + ":" + age);
        return "success";
    }


    //2.实体参数
    @RequestMapping(value = "/simplePojo",method = RequestMethod.POST)
    @Operation(description = "true")
    public String simplePojo(User user){
        System.out.println(user);
        return "OK";
    }
    @RequestMapping(value = "/complexPojo",method = RequestMethod.POST)
    @Operation(description = "complex")
    public String complexPojo(@RequestBody User user){
        System.out.println(user);
        return "OK";
    }

    //3.数组集合参数
    @RequestMapping(value = "/arrayParm",method = RequestMethod.GET)
    @Operation(description = "array")
    public String arrayParm(String[] hobby){
        System.out.println(Arrays.toString(hobby));
        return "OK";
    }

    @RequestMapping(value = "/listParm",method = RequestMethod.GET)
    @Operation(description = "list")
    public String listParm(@RequestParam List<String> hobby){
        System.out.println(hobby);
        return "OK";
    }

    //4.日期参数
    @RequestMapping(value = "/dateParm",method = RequestMethod.GET)
    @Operation(description = "date")
    public String dateParm(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime updateTime){
        System.out.println(updateTime);
        return "OK";
    }

    //5.jason参数
    @RequestMapping(value = "/jasonParm",method = RequestMethod.POST)
    @Operation(description = "jason")
    public String jasonParm(@RequestBody User user){
        System.out.println(user);
        return "OK";
    }

    //6.路径参数
    @RequestMapping(value = "/path/{id}/{name}",method = RequestMethod.GET)
    @Operation(description = "jason")
    public String pathParm(@PathVariable Integer id,@PathVariable String name){
        System.out.println(id);
        System.out.println(name);
        return "OK";
    }
}
